package top.deepdesigner.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import top.deepdesigner.pojo.DtsBrand;
import top.deepdesigner.service.DtsBrandService;

import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/22 星期四 14:30:01 <br/>
 */
@Service
public class DtsBrandServiceImpl extends ServiceImpl<BaseMapper<DtsBrand>, DtsBrand> implements DtsBrandService {
  @Override
  public Long getBrandCount() {
    return this.count();
  }

  @Override
  public List<DtsBrand> getBrandList(Integer page, Integer limit, String sort, String order, String id, String name) {
    QueryWrapper<DtsBrand> wrapper = new QueryWrapper<>();
    wrapper
        .eq("deleted", 0)
        .like(!StringUtils.isEmpty(id), "id", id)
        .like(!StringUtils.isEmpty(name), "name", name)
        .orderBy(true, "asc".equalsIgnoreCase(order), sort);
    Page<DtsBrand> userPage = new Page<>(page, limit);
    return this.page(userPage, wrapper).getRecords();
  }

  @Override
  public List<DtsBrand> getBrandList() {
    return this.list();
  }

  @Override
  @Transactional
  public DtsBrand createBrand(DtsBrand dtsBrand) {
    this.save(dtsBrand);
    return dtsBrand;
  }

  @Override
  @Transactional
  public DtsBrand updateBrand(DtsBrand dtsBrand) {
    this.update(dtsBrand, new LambdaUpdateWrapper<DtsBrand>().eq(DtsBrand::getId, dtsBrand.getId()));
    return dtsBrand;
  }

  @Override
  @Transactional
  public Boolean deleteBrand(DtsBrand dtsBrand) {
    dtsBrand.setDeleted(true);
    return this.update(dtsBrand, new LambdaUpdateWrapper<DtsBrand>()
        .eq(true, DtsBrand::getId, dtsBrand.getId()));
  }
}
